package org.emet.jee.console.dao.security;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.emet.jee.console.entity.security.User;
import org.emet.jee.console.web.form.UserForm;
import org.emet.jee.core.dao.Page;
import org.emet.jee.core.dao.hibernate.HibernateDao;
import org.springframework.stereotype.Repository;

/**
 * 用户对象的泛型DAO类.
 */
@Repository
public class UserDao extends HibernateDao<User, Long> {

	@SuppressWarnings("unchecked")
	public Page<User> search(Page<User> page, UserForm criteria) {
		String hql = "from User where 1=1 ";
		Map paraMap = new HashMap();
		if (!StringUtils.isBlank(criteria.getLoginName())) {
			hql += " and loginName like :loginName";
			paraMap.put("loginName", "%" + criteria.getLoginName() + "%");
		}
		if (!StringUtils.isBlank(criteria.getName())) {
			hql += " and name like :name";
			paraMap.put("name", "%" + criteria.getName() + "%");
		}
		if (!StringUtils.isBlank(criteria.getDepartment())) {
			hql += " and department.id = :department ";
			paraMap.put("department", Long.valueOf(criteria.getDepartment()));
		}
		if (!StringUtils.isBlank(criteria.getRole())) {
			hql += " and :role in elements(roleList)";
			paraMap.put("role", criteria.getRole());
		}
		if (!StringUtils.isBlank(criteria.getStaffNumber())) {
			hql += " and :staffNumber = :SN";
			paraMap.put("SN", criteria.getStaffNumber());
		}
		return findPage(page, hql, paraMap);
	}

}
